package com.winning.pmph.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.KnowledgeSyncRecord;
import com.winning.pmph.entity.KnowledgeSyncStatus;
import com.winning.pmph.entity.PlatformKnowledgeLib;
import com.winning.pmph.entity.ReleasedKnowledge;
import com.winning.pmph.mapper.KnowledgeSyncRecordMapper;
import com.winning.pmph.mapper.KnowledgeSyncStatusMapper;
import com.winning.pmph.utils.LocalDateTimeUtil;
import com.winning.pmph.utils.PMPHAppUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class KnowledgeSyncStatusService extends ServiceImpl<KnowledgeSyncStatusMapper, KnowledgeSyncStatus> {

    @Resource
    private KnowledgeSyncRecordMapper knowledgeSyncRecordMapper;
    @Resource
    private PlatformKnowledgeLibService platformKnowledgeLibService;
    @Resource
    private RocketMQProducerService rocketMQProducerService;

    public void insertSync(Map<String, String> map, boolean sendFlag) {
        KnowledgeSyncRecord knowledgeSyncRecord = new KnowledgeSyncRecord();
        knowledgeSyncRecord.setKnowledgeId(map.get("knowledgeId"));
        knowledgeSyncRecord.setPlatformId(map.get("syncPlatform"));
        knowledgeSyncRecord.setOperatedBy(map.get("userName"));
        knowledgeSyncRecord.setOperatedTime(LocalDateTimeUtil.getNowAsString());
        KnowledgeSyncStatus knowledgeSyncStatus = JSON.parseObject(JSON.toJSONString(knowledgeSyncRecord), KnowledgeSyncStatus.class);
        knowledgeSyncStatus.setKnowledgeLibId(map.get("knowledgeLibId"));
        // 改状态;
        if (sendFlag) {
            knowledgeSyncRecord.setSyncFlag(KnowledgeSyncRecord.SYNC_STATUS_SUCCESS);
            knowledgeSyncStatus.setKind(KnowledgeSyncStatus.KIND_SUCCESS);
        } else {
            knowledgeSyncRecord.setSyncFlag(KnowledgeSyncRecord.SYNC_STATUS_FAILURE);
            knowledgeSyncStatus.setKind(KnowledgeSyncStatus.FAILED_KIND_PRODUCER);
        }
        // 记录同步日志, 同步状态;
        knowledgeSyncRecordMapper.saveBean(knowledgeSyncRecord);
        // 先删除再新增;
        getBaseMapper().deleteBeansByWherePart(
                "knowledge_id = '" + knowledgeSyncStatus.getKnowledgeId() +
                        "' and platform_id = '" + knowledgeSyncStatus.getPlatformId() + "'");
        save(knowledgeSyncStatus);
    }

    public void correctKnowledge(ReleasedKnowledge knowledge) {
        List<PlatformKnowledgeLib> platformKnowledgeLibList = platformKnowledgeLibService.lambdaQuery().eq(PlatformKnowledgeLib::getKnowledgeLibId, knowledge.getKnowledgeLibId()).list();
        for (PlatformKnowledgeLib platformKnowledgeLib : platformKnowledgeLibList) {
            // 如果自动同步, 则立即同步;
            if (platformKnowledgeLib.isAutosyncFlag()) {
                rocketMQProducerService.sendKnowledge(platformKnowledgeLib.getPlatformId(), knowledge, PMPHAppUtil.getCurrentUserEnName());
            }
            // 如果非自动同步, 则修改同步状态为待同步;
            else {
                update(Wrappers.lambdaUpdate(KnowledgeSyncStatus.class)
                        .eq(KnowledgeSyncStatus::getPlatformId, platformKnowledgeLib.getPlatformId())
                        .eq(KnowledgeSyncStatus::getKnowledgeId, knowledge.getId())
                        .set(KnowledgeSyncStatus::getOperatedTime, LocalDateTimeUtil.getNowAsString())
                        .set(KnowledgeSyncStatus::getKind, KnowledgeSyncStatus.FAILED_KIND_WAITING));
            }
        }
    }
}